package socket_net_code.udp;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;

public class Demo2_Receive {
    public static void main(String[] args) throws IOException {
        //创建Socket相当于码头
        DatagramSocket socket = new DatagramSocket(6666);
        //创建Packet相当于创建集装箱
        DatagramPacket packet = new DatagramPacket(new byte[1024], 1024);
        //接货，接收数据
        socket.receive(packet);
        //获取数据
        byte[] arr = packet.getData();
        //获取有效的字节个数
        int len = packet.getLength();
        //获取ip地址
        String ip = packet.getAddress().getHostAddress();
        //获取端口号
        int port = packet.getPort();
        System.out.println("ip地址："+ip + " 端口号：" + port + ":" + "收到消息："+new String(arr, 0, len));

        //socket.close();
    }

}
